package com.shruijin.wy.proxy.dynamic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;


/**
 * 参考文献： https://www.cnblogs.com/qlqwjy/p/7550609.html
 */

public class ProxyFactory  implements MethodInterceptor {

    Logger LOGGER = LoggerFactory.getLogger(this.getClass());
    private Object target;

    public ProxyFactory(Object target) {
        this.target = target;
    }

    //给目标对象创建一个代理对象
    public Object getProxyInstance(){
        //1.工具类
        Enhancer en = new Enhancer();
        //2.设置父类
        en.setSuperclass(target.getClass());
        //3.设置回调函数
        en.setCallback(this);
        //4.创建子类(代理对象)
        return en.create();
    }

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        LOGGER.info("开始事务...");
        //执行目标对象的方法
        Object returnValue = method.invoke(target, args);
        LOGGER.info("提交事务...");
        return returnValue;
    }
}
